package dao

import (
	"context"
	"encoding/json"
	"sale-admin/internal/app/web/schema"
	"sale-admin/internal/dal"
	"sale-admin/internal/define"
	"sale-admin/internal/models"
	"time"

	"192.168.1.75/go-pkg/helper"
)

type Payment struct{}

// 新增
func (Payment) Create(ctx context.Context, query *dal.Query, merchantID, paymentType string,
	originID int, name, config string) (count int64, err error) {
	m := query.MaPayment
	tx := m.WithContext(ctx)

	var data models.MaPayment
	// id 自增
	data.MerchantID = merchantID
	data.PaymentType = paymentType
	if originID == 0 {
		id, _ := helper.UUID{}.Int64()
		data.OriginID = int(id)
	} else {
		data.OriginID = originID
	}
	data.Name = name
	data.Config = config
	data.CreatedAt = time.Now().Unix()
	err = tx.Create(&data)
	if err != nil {
		return 0, err
	}
	return 1, nil
}

// 编辑
func (Payment) Update(ctx context.Context, query *dal.Query, merchantID, paymentType string,
	OriginID int, name, config string) (count int64, err error) {
	m := query.MaPayment
	tx := m.WithContext(ctx)

	var data models.MaPayment
	data.MerchantID = merchantID
	data.PaymentType = paymentType
	data.Name = name
	data.Config = config
	ret, err := tx.Where(m.OriginID.Eq(OriginID)).Updates(&data)
	return ret.RowsAffected, nil
}

// 删除
func (Payment) Delete(ctx context.Context, query *dal.Query, merchantID, paymentType string, OriginID int) (err error) {
	m := query.MaPayment
	tx := m.WithContext(ctx)
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if paymentType != "" {
		tx = tx.Where(m.PaymentType.Eq(paymentType))
	}
	if OriginID != 0 {
		tx = tx.Where(m.OriginID.Eq(OriginID))
	}
	tx.Delete()
	return
}

// 支付方式 -- 微信
func (Payment) GetWechatPay(ctx context.Context, query *dal.Query, merchantID string, OriginID int) (
	res []schema.PaymentWechatRsp, err error,
) {
	m := query.MaPayment
	tx := m.WithContext(ctx).
		Where(m.PaymentType.Eq(define.PaymentType_Wechat))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if OriginID != 0 {
		tx = tx.Where(m.OriginID.Eq(OriginID))
	}
	list, err := tx.Find()
	for _, v := range list {
		var item schema.PaymentWechatRsp
		item.ID = v.OriginID
		item.PaymentID = v.PaymentType
		item.ShopID = v.MerchantID
		item.Name = v.Name
		config := schema.PaymentWechat{}
		json.Unmarshal([]byte(v.Config), &config)
		item.WeChatMchID = config.WeChatMchID
		item.WeChatAppID = config.WeChatAppID
		item.WeChatAppKey = config.WeChatAppKey
		item.CertFile = config.CertFile
		res = append(res, item)
	}
	return
}

// 支付方式 -- 小天才
func (Payment) GetXiaotiancaiPay(ctx context.Context, query *dal.Query, merchantID string, OriginID int) (
	res []schema.PaymentSmallRsp, err error,
) {
	m := query.MaPayment
	tx := m.WithContext(ctx).
		Where(m.PaymentType.Eq(define.PaymentType_XiaoTianCai))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if OriginID != 0 {
		tx = tx.Where(m.OriginID.Eq(OriginID))
	}
	list, err := tx.Find()
	for _, v := range list {
		var item schema.PaymentSmallRsp
		item.ID = v.OriginID
		item.PaymentID = v.PaymentType
		item.ShopID = v.MerchantID
		item.Name = v.Name
		config := schema.PaymentWechat{}
		json.Unmarshal([]byte(v.Config), &config)
		item.WeChatMchID = config.WeChatMchID
		item.WeChatAppID = config.WeChatAppID
		item.WeChatAppKey = config.WeChatAppKey
		item.CertFile = config.CertFile
		res = append(res, item)
	}
	return
}

// 支付方式 -- 支付宝
func (Payment) GetAliPay(ctx context.Context, query *dal.Query, merchantID string, OriginID int) (res []schema.PaymentAlipayRsp, err error) {
	m := query.MaPayment
	tx := m.WithContext(ctx).
		Where(m.PaymentType.Eq(define.PaymentType_Alipay))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if OriginID != 0 {
		tx = tx.Where(m.OriginID.Eq(OriginID))
	}
	list, err := tx.Find()
	for _, v := range list {
		var item schema.PaymentAlipayRsp
		item.ID = v.OriginID
		item.PaymentID = v.PaymentType
		item.ShopID = v.MerchantID
		item.Name = v.Name
		config := schema.PaymentAlipay{}
		json.Unmarshal([]byte(v.Config), &config)
		item.AliPayAppID = config.AliPayAppID
		item.AliPayPrivateKey = config.AliPayPrivateKey
		res = append(res, item)
	}
	return
}

// 支付方式 -- 大兴
func (Payment) GetDaxingPay(ctx context.Context, query *dal.Query, merchantID string, OriginID int) (res []schema.PaymentDxRsp, err error) {
	m := query.MaPayment
	tx := m.WithContext(ctx).
		Where(m.PaymentType.Eq(define.PaymentType_DaXing))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if OriginID != 0 {
		tx = tx.Where(m.OriginID.Eq(OriginID))
	}
	list, err := tx.Find()
	for _, v := range list {
		var item schema.PaymentDxRsp
		item.ID = v.OriginID
		item.PaymentID = v.PaymentType
		item.ShopID = v.MerchantID
		item.Name = v.Name
		config := schema.PaymentDx{}
		json.Unmarshal([]byte(v.Config), &config)
		item.DxAppID = config.DxAppID
		item.DxAppKey = config.DxAppKey
		item.DxSellerID = config.DxSellerID
		item.DxMerchantID = config.DxMerchantID
		item.DxShopID = config.DxShopID
		res = append(res, item)
	}
	return
}

// 支付方式 -- 聚合
func (Payment) GetJuhePay(ctx context.Context, query *dal.Query, merchantID string, OriginID int) (res []schema.PaymentJhRsp, err error) {
	m := query.MaPayment
	tx := m.WithContext(ctx).
		Where(m.PaymentType.Eq(define.PaymentType_JuHe))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if OriginID != 0 {
		tx = tx.Where(m.OriginID.Eq(OriginID))
	}
	list, err := tx.Find()
	for _, v := range list {
		var item schema.PaymentJhRsp
		item.ID = v.OriginID
		item.PaymentID = v.PaymentType
		item.ShopID = v.MerchantID
		item.Name = v.Name
		config := schema.PaymentJh{}
		json.Unmarshal([]byte(v.Config), &config)
		item.JhMerchantNum = config.JhMerchantNum
		item.JhApiKey = config.JhApiKey
		res = append(res, item)
	}
	return
}

// 支付方式 -- stripe
func (Payment) GetStripePay(ctx context.Context, query *dal.Query, merchantID string, OriginID int) (
	res []schema.PaymentStripeRsp, err error) {
	m := query.MaPayment
	tx := m.WithContext(ctx).
		Where(m.PaymentType.Eq(define.PaymentType_Stripe))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if OriginID != 0 {
		tx = tx.Where(m.OriginID.Eq(OriginID))
	}
	list, err := tx.Find()
	for _, v := range list {
		var item schema.PaymentStripeRsp
		item.ID = v.OriginID
		item.PaymentID = v.PaymentType
		item.ShopID = v.MerchantID
		item.Name = v.Name
		config := schema.PaymentStripe{}
		json.Unmarshal([]byte(v.Config), &config)
		item.StripePrivKey = config.StripePrivKey
		item.StripePubKey = config.StripePubKey
		res = append(res, item)
	}
	return
}

// 支付方式 -- POS
func (Payment) GetPOSPay(ctx context.Context, query *dal.Query, merchantID string, OriginID int) (res []schema.PaymentPosRsp, err error) {
	m := query.MaPayment
	tx := m.WithContext(ctx).
		Where(m.PaymentType.Eq(define.PaymentType_POS))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if OriginID != 0 {
		tx = tx.Where(m.OriginID.Eq(OriginID))
	}
	list, err := tx.Find()
	for _, v := range list {
		var item schema.PaymentPosRsp
		item.ID = v.OriginID
		item.PaymentID = v.PaymentType
		item.ShopID = v.MerchantID
		item.Name = v.Name
		res = append(res, item)
	}
	return
}
